import {
  isNull
} from '@/utils/util'

class EnumItem {
  constructor(object) {
    this.key = object.key
    this.value = object.value
    this.name = object.name
  }
  equal(keyOrValue) {
    return this.key === keyOrValue || this.value === keyOrValue
  }
}
EnumItem.prototype.valueOf = function() {
  return this.value
}
class Enum {
  constructor(enumArr) {
    this._enum = {}
    for (const item of enumArr) {
      this._enum[item.key] = new EnumItem(item)
      Object.defineProperty(this, item.key, {
        value: this._enum[item.key]
      })
    }
  }
  getEnum() {
    return this._enum
  }
  get(keyOrValue) {
    if (!isNull(this._enum[keyOrValue])) {
      return this._enum[keyOrValue]
    } else {
      for (const key in this._enum) {
        if (this._enum.hasOwnProperty(key)) {
          if (keyOrValue === this._enum[key].value) {
            return this._enum[key]
          }
        }
      }
      return null
    }
  }
  getName(keyOrValue) {
    const tempEnum = this.get(keyOrValue)
    return tempEnum ? tempEnum.name : null
  }
  valueIsIn(value, keyOrValues = []) {
    let temp
    for (const keyOrValue of keyOrValues) {
      temp = this.get(keyOrValue)
      if (temp && temp.value === value) {
        return true
      }
    }
    return false
  }
  valueIsNotIn(value, keyOrValues = []) {
    let temp
    for (const keyOrValue of keyOrValues) {
      temp = this.get(keyOrValue)
      if (temp && temp.value === value) {
        return false
      }
    }
    return true
  }
}
Enum.prototype.valueOf = function() {
  return this._enum
}
export default Enum
export const PASS_DATA = new Enum([{
  key: '0',
  value: 'ydz',
  name: '已对账'
},
{
  key: '1',
  value: 'wdz',
  name: '未对账'
}
// {
//   key: '2',
//   value: 'yc',
//   name: '异常'
// },
// {
//   key: '3',
//   value: 'ycz',
//   name: '冲正处理'
// }
])
export const PAY_TYPE = new Enum([{
  key: '1',
  value: 'wx',
  name: '微信'
}, {
  key: '2',
  value: 'zfb',
  name: '支付宝'
}])
export const ORDER_STATUS = new Enum([{
  key: '1',
  value: '1',
  name: '未完成'
},
{
  key: '2',
  value: '2',
  name: '已支付待发货'
},
{
  key: '3',
  value: '3',
  name: '已发货'
}, {
  key: '4',
  value: '4',
  name: '待签收'
}, {
  key: '5',
  value: '5',
  name: '已完成'
}, {
  key: '6',
  value: '6',
  name: '支付异常'
}, {
  key: '7',
  value: '7',
  name: '用户取消关闭订单'
}, {
  key: '8',
  value: '8',
  name: '支付超时关闭订单'
}, {
  key: '9',
  value: '9',
  name: '后台手动关闭订单'
}
])

export const ACCOUNT_STATE = new Enum([{
  key: '1',
  value: '1',
  name: true
},
{
  key: '2',
  value: '0',
  name: false
}])
